package sg.edu.nus.iss.jass;

import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.junit.AfterClass;
import org.junit.BeforeClass;

import sg.edu.nus.iss.jass.controller.MainController;
import sg.edu.nus.iss.jass.gui.MainFrame;
import sg.edu.nus.iss.jass.persistence.JobSeekerDAO;
import sg.edu.nus.iss.jass.privilege.SessionContext;
import sg.edu.nus.iss.jass.privilege.User;

public class BasicTestCase  {

	private JobSeekerDAO dao = new JobSeekerDAO();

	protected static MainController mainController;

	public BasicTestCase() {

		super();
		MainFrame myExample = new MainFrame();
		myExample.setLocationRelativeTo(null);
		myExample.setVisible(false);
		mainController = MainController.getInstance();
		mainController.init(myExample);
	}

	public void clearData() {

		String PATH_SEP = System.getProperty("file.separator");
		String folderName = "junitdata";
		String storeFolder = "";
		try {
			storeFolder = (new File(".").getCanonicalPath().concat(PATH_SEP).concat(folderName).concat(PATH_SEP));
		} catch (IOException e) {
			fail(e.getMessage());
		}

		File file = new File(storeFolder);
		Set<String> predefinedData = new HashSet<String>();
		predefinedData.add("sg.edu.nus.iss.jass.dto.SkillReference");
		predefinedData.add("sg.edu.nus.iss.jass.dto.Company");
		predefinedData.add("sg.edu.nus.iss.jass.dto.GeographicalLocation");
		// create folder for store testing data
		if (!file.exists()) {
			file.mkdir();
		} else {
			// delete all old data
			File fs[] = file.listFiles();
			if (fs!=null) {
				for (File f : fs) {
					if (!predefinedData.contains(f.getName())) {
						f.delete();
					}
				}
			}
		}
		dao.setTestFolder(folderName);
		dao.clearCache();
	}

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {

	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		
	}

	public String getMessge() {

		return this.mainController.getMainframe().getFunctionPanel().getHeaderPanel().getMsgText();
	}

	public void setUser(User user) {

		SessionContext.setUser(user);
	}
}
